let href = window.location.href;
let _c = id => document.createEvent(id)
let bijia = document.querySelector('.bijia')
let pro = document.querySelector('.pro')
let details = document.querySelector('#details')
let comment = document.querySelector('#comment')

function parseQuery(url) {
    var queryObj = {};
    var reg = /[?&]([^=&#]+)=([^&#]*)/g;
    var querys = url.match(reg);
    if (querys) {
        for (var i in querys) {
            var query = querys[i].split('=');
            var key = query[0].substr(1),
                value = query[1];
            queryObj[key] ? queryObj[key] = [].concat(queryObj[key], value) : queryObj[key] = value;
        }
    }
    return queryObj;
}

let id = parseQuery(href).categoryid
console.log(id);
/* 商品详情接口 */
ajax({
        url: "http://chst.vip:1234/api/getproduct?productid=" + id
    }).then(res => {
        let det = res.result[0]
            /*  console.log(det); */
        let catid = res.result[0].categoryId;
        pro.innerHTML = `${det.productName}`
        let html = ''
        html = `<ul>
       <li>
          ${det.productImg}
       </li>
       <li><span>
       ${det.productName}
       </span></li>
       <li><span class="left">当前最低价￥6000哒不溜</span><span class="right">优选评论8266条(都是假的)</span></li>
       <li>${det.bjShop}</li>
   </ul>`
        details.innerHTML = html
            /* 导航栏接口 */
        ajax({
            url: 'http://chst.vip:1234/api/getcategorybyid?categoryid=' + catid
        }).then(res => {
            let nav = res.result[0]
                /*  console.log(nav);
                 console.log(bijia); */
            bijia.innerHTML = `${nav.category}&gt;`

            bijia.href = `bijia.html?categoryid=${nav.categoryId}`
        })

        let proid = res.result[0].productId;

        /* 评论接口 */
        ajax({
            url: "http://chst.vip:1234/api/getproductcom?productid=" + proid
        }).then(res => {
            let cen = res.result
            console.log(cen);
            comment.innerHTML = comm(cen)
        })
    })
    /* 评论渲染函数  只有一次返回多条数据才需要用forEach */
function comm(cen) {
    let html2 = '';
    cen.forEach(item => {
        html2 += `<li>
     <div class="top">
         <span>${item.comName}</span>
         <span>${item.comTime}</span>
     </div>
     <div class="center">
         <span>${item.comContent}</span>
     </div>
     <div class="bottom">
         <span>${item.comFrom}</span>
     </div>
 </li>`
    });
    return html2
}